package config;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "natappvip.liujinliang.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
	
	private static final String DEFAULT_ENCDOING = "UTF-8";
	
	/**
	 * 配置试图解析器
	 * @param ViewResolverRegistry 用来注册试图解析器
	 */
	@Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
		
		/**
		 *  创建一个管理所有试图解析器却不干任何解析视图工作的老大
		 *  并且带了一个贴身小弟用来处理Controller返回Json 
		 */
        registry.enableContentNegotiation(new MappingJackson2JsonView());
        
        /**
         * 创建 FreeMarker 作为我们的视图解析器来解析所有视图
         * 这样看来，MappingJackson2JsonView不会起作用，
         * 但是我们可以借此机会看看MappingJackson2JsonView
         */
        registry.freeMarker().prefix("").suffix(".ftl");
    }
	
	@Bean
    public FreeMarkerConfigurer freeMarkerConfigurer() {
		
		/** 创建 FreeMarker 的配置类 */
        FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
        
        /** 
         * 常见的两种视图解析器，在路由（寻路）方面还是有一些区别的
         * FreeMarker 路由：TemplateLoaderPath + prefix + Controller_return_value + suffix
         * InternalViewResolver 路由： prefix + Controller_return_value + suffix
         */
        configurer.setTemplateLoaderPath("/WEB-INF/views");
        return configurer;
    }
	
	/** 
	 * 静态文件处理的方式有两种：
	 * tomcat来处理 或者 Spring来处理
	 * */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		/**
		 * addResourceLocations 用来标注静态资源所在的路径
		 * addResourceHandler 用来拦截所有 /resources/** 的请求
		 */
		registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/static/");
	}
	
	/**
	 * 邮件发送注意事项：
	 * 引入 jar 包避免引入 javax-mail-api
	 * 在 163 邮箱中开启 smtp 服务器
	 * 设置 163 邮箱密码应该使用授权码
	 */
	@Bean
	public JavaMailSender javaMilSenderImpl() {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setHost("smtp.163.com");
		mailSender.setDefaultEncoding(DEFAULT_ENCDOING);
		mailSender.setPort(25);
		mailSender.setUsername("17625922937@163.com");
		mailSender.setPassword("Ljl1994");
		return mailSender;
	}
	
	/**
	 * 文件上传
	 */
	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver resolver = new CommonsMultipartResolver();
		resolver.setDefaultEncoding(DEFAULT_ENCDOING);
		resolver.setMaxUploadSize(1048576000);
		resolver.setResolveLazily(true);
		return resolver;
	}
	
	/**
	 * 应用属性文件的读入
	 * 注意：这里的 static 不可以缺少
	 */
	@Bean
	public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
		PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
		ClassPathResource resource = new ClassPathResource("config.properties");
		configurer.setLocation(resource);
		return configurer;
	}
	
}
